package com.zhongke.home.ui.activity

import android.content.Intent
import android.os.Bundle
import android.util.Log
import androidx.core.view.isVisible
import com.alibaba.android.arouter.launcher.ARouter
import com.elvishew.xlog.XLog
import com.zhongke.common.base.activity.ZKBaseActivity
import com.zhongke.common.ext.*
import com.zhongke.common.imageloader.CornerType
import com.zhongke.common.imageloader.loadCorner
import com.zhongke.common.imageloader.loadNormal
import com.zhongke.common.imageloader.loadRound
import com.zhongke.common.utils.ZKToastUtils
import com.zhongke.common.utils.singleClick
import com.zhongke.common.utils.toast
import com.zhongke.home.BR
import com.zhongke.home.R
import com.zhongke.home.databinding.ActivityHomeBinding
import com.zhongke.home.ui.viewmodel.ZKHomeViewModel
import java.lang.Math.max
import kotlin.math.log

/**
 * @author: 陈元旺
 * @date: 2022/5/6
 * @description：
 */
class ZKHomeActivity : ZKBaseActivity<ActivityHomeBinding, ZKHomeViewModel>() {

    val url =
        "https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/9009917618ee4dad91413229e319a341~tplv-k3u1fbpfcp-zoom-in-crop-mark:1304:0:0:0.awebp"

    override fun initContentView(savedInstanceState: Bundle?): Int {
        return R.layout.activity_home
    }

    override fun initVariableId(): Int {
        return BR.viewModel
    }

    override fun initData() {
        super.initData()

        binding.btnTest.setOnClickListener {
//            viewModel.getVersion()
            showDialog("请稍后...")
        }
        binding.slider.addOnChangeListener { slider, value, _ ->
            val cornerType = getCornerType()
            binding.homeImageview.loadCorner(url, value.toInt(), cornerType)
            if (value == slider.valueTo) {
                startActivity<ZKTestTitleActivity>()
            }
        }
        initImageView()
        binding.tabMode.setOnClickListener {
            startActivity<ZKTabActivity>()
        }
    }


    override fun onCreateObserver() {
        super.onCreateObserver()
        viewModel.run {
            observeState(versionData) {
                onSuccess {
                    toast("请求接口成功")
                }
                onFailed {
                    toast("请求接口失败")
                }
            }
        }
    }

    private fun initImageView() {
        binding.run {
            btDefault.singleClick {
                homeImageview.loadNormal(R.drawable.img_loader)
            }

            btRound.singleClick {
                homeImageview.loadRound(R.drawable.img_loader)
            }

            btCorner.singleClick(500) {
                val cornerType = getCornerType()
                homeImageview.loadCorner(url, 10, cornerType) {
                    placeholder(R.drawable.ic_launcher_background)
                }
            }
        }
    }

    private fun getCornerType(): Int {
        return binding.let {
            if (it.checkAll.isChecked) {
                return CornerType.ALL
            }
            if (it.checkTop.isChecked) {
                return CornerType.TOP
            }
            if (it.checkBottom.isChecked) {
                return CornerType.BOTTOM
            }
            if (it.checkLeft.isChecked) {
                return CornerType.LEFT
            }
            if (it.checkRight.isChecked) {
                return CornerType.RIGHT
            }
            CornerType.ALL
        }
    }
}